﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RoughJsConsole;
using System.IO;
using System.Diagnostics;

namespace JsEditor
{
    /// <summary>
    /// Interaction logic for DebugWindow.xaml
    /// </summary>
    public partial class DebugWindow : DockingLibrary.DockableContent
    {
        public DebugWindow()
        {
            InitializeComponent();
            //this.shellControl.
        }

        public void InitConsole()
        {

            shellControl.Prompt = "";

            shellControl.CommandEntered += new UILibrary.EventCommandEntered(shellControl1_CommandEntered);
            
            
            shellControl.WriteText("js> ");
            shellControl.OnCommandEntered("js> alert('打开控制台');");
            
        }
        public void RunFile(String fileFullName)
        {
            shellControl.WriteText("load('" + fileFullName + "');\r\n");
            shellControl.OnCommandEntered("load('" + fileFullName + "');");
        }

        public void Clear()
        {
            shellControl.Clear();
            shellControl.WriteText("js> ");
            //shellControl.OnCommandEntered("js> alert('ShellOpen');");
        }

        private ProcessStartInfo startInfo = null;
        private Process p = null;
        void shellControl1_CommandEntered(object sender, UILibrary.CommandEnteredEventArgs e)
        {
            string command = e.Command;
            if(command.StartsWith("js> "))
            {
                command = command.Substring(3);
            }
            //if (!ProcessInternalCommand(command))
            //{
            Init();
            StreamWriter ww = p.StandardInput;
            ww.WriteLine(command);
            ww.Flush();
            
           // p.
            
            //p.WaitForExit(100);
           // p.
            //if (p.StandardOutput.Peek() > 0)
            //{
          //StreamReader re =   p.StandardOutput;
         
               // string output =re .ReadLine();
            //re.
             //   p.StandardOutput.Close();
                //string error = p.StandardError.ReadLine(); ;
                //p.WaitForExit();
                //if (output.Length != 0)
                //    shellControl.WriteText(output);
           // }
            //else if (error.Length != 0)
            //    shellControl.WriteText(error);
            // }
        }

        public void Init()
        {//初始化控制台
            if (startInfo == null)
            {
                startInfo = new ProcessStartInfo(AppDomain.CurrentDomain.BaseDirectory + "RoughJsConsole.exe");
                startInfo.Arguments = "-e";
                startInfo.RedirectStandardError = true;
                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardInput = true;
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = true;


            }

            if (p == null)
            {
                p = Process.Start(startInfo);
                p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
                p.BeginOutputReadLine();

            }
        }
        public void ReStart()
        {
            this.Clear();
            Init();
            //shellControl.WriteText("js> ");
            shellControl.OnCommandEntered("js> alert('打开控制台');");
        }

        void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            string output = e.Data;
            if (output.Length != 0)
            {
                NewTaskDelegate task = newTask;
                this.Dispatcher.BeginInvoke(task, new Object[] { output });

                //this.Dispatcher.BeginInvoke(
                // delegate
                // {
                   
                // }, null);
            }
        }

        private delegate void NewTaskDelegate(String output);
        private void newTask(String output)
        {
            if (output == "js> ")
                shellControl.WriteText(output );
            else
            shellControl.WriteText(output+"\r\n");
            //shellControl.WriteText("\n");
            //shellControl.WriteText("js> ");
        }

        


       
    }
}
